package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tb.lmt.domain.Constant;
import com.ruoyi.tb.lmt.domain.bo.ClientLogBo;
import com.ruoyi.tb.lmt.domain.bo.ClientRegistryBo;
import com.ruoyi.tb.lmt.domain.bo.KeepAliveBo;
import com.ruoyi.tb.lmt.domain.vo.ClientRegistryVo;
import com.ruoyi.tb.lmt.service.IClientLogService;
import com.ruoyi.tb.lmt.service.IClientRegistryService;
import com.ruoyi.tb.lmt.service.IClientService;
import com.ruoyi.tb.lmt.service.ILifeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zoubeibei
 * @date 2023年12月13日 15:00
 */
@RequiredArgsConstructor
@Service
public class LifeServiceImpl implements ILifeService {

    private final IClientRegistryService iClientRegistryService;

    private final IClientLogService iClientLogService;

    private final IClientService iClientService;

    public void clientAlive(){
        ClientRegistryBo bo = new ClientRegistryBo();
        bo.setStatus(Constant.ON);
        List<ClientRegistryVo> clientRegistryVos =
            iClientRegistryService.queryList(bo);

        for(ClientRegistryVo temp : clientRegistryVos){
            try{
                String clientAddress = temp.getClientAddress();
                String url = clientAddress+"/bjclient/alive";
                String ret = HttpUtil.get(url);
                if(!"true".equals(ret)){
                    throw new ServiceException("下线");
                }
            }catch (Exception e){
                ClientRegistryBo bean = BeanUtil.toBean(temp, ClientRegistryBo.class);
                bean.setStatus(Constant.OFF);
                iClientRegistryService.updateByBo(bean);
                //登记下线
                offline(temp.getClientCode(), temp.getClientName());
            }
        }
    }

    @Override
    public Boolean keepAlive(KeepAliveBo keepAliveBo) {
        if(iClientRegistryService.keepAlive(keepAliveBo)){
            //登记上线
            online(keepAliveBo.getClientCode(), keepAliveBo.getClientName());
        }
        return true;
    }

    @Override
    public void pullingClient() {
        //查询所有在线的
        ClientRegistryBo bo = new ClientRegistryBo();
        bo.setStatus(Constant.ON);
        List<ClientRegistryVo> clientRegistryVos = iClientRegistryService.queryList(bo);

        for(ClientRegistryVo temp : clientRegistryVos){
            String limits = temp.getLimits();
            if(StringUtils.isEmpty(limits)){
                continue;
            }
            //根据权限进行拉取
            if(limits.contains("10002")){
                if(iClientService.get10002(temp)){
                    operation(temp.getClientCode(), temp.getClientName(), "10002拉取", "成功");
                }else {
                    operation(temp.getClientCode(), temp.getClientName(), "10002拉取", "失败");
                }
            }
            if(limits.contains("10003")){
                if(iClientService.get10003(temp)){
                    operation(temp.getClientCode(), temp.getClientName(), "10003拉取", "成功");
                }else {
                    operation(temp.getClientCode(), temp.getClientName(), "10003拉取", "失败");
                }
            }
            if(limits.contains("10006")){
                if(iClientService.get10006(temp)){
                    operation(temp.getClientCode(), temp.getClientName(), "10006拉取", "成功");
                }else {
                    operation(temp.getClientCode(), temp.getClientName(), "10006拉取", "失败");
                }
            }
        }
    }

    @Override
    public void backupsClient() {
        //查询所有在线的
        ClientRegistryBo bo = new ClientRegistryBo();
        bo.setStatus(Constant.ON);
        List<ClientRegistryVo> clientRegistryVos = iClientRegistryService.queryList(bo);

        for(ClientRegistryVo temp : clientRegistryVos){
            if(iClientService.get10004(temp)){
                operation(temp.getClientCode(), temp.getClientName(), "10004备份", "成功");
            }else{
                operation(temp.getClientCode(), temp.getClientName(), "10004备份", "失败");
            }
            if(iClientService.get10005(temp)){
                operation(temp.getClientCode(), temp.getClientName(), "10005备份", "成功");
            }else {
                operation(temp.getClientCode(), temp.getClientName(), "10005备份", "失败");
            }
            if(iClientService.get10007(temp)){
                operation(temp.getClientCode(), temp.getClientName(), "10007备份", "成功");
            }else{
                operation(temp.getClientCode(), temp.getClientName(), "10007备份", "失败");
            }
        }
    }

    @Override
    public void pullingClientFile() {
        //查询所有在线的
        ClientRegistryBo bo = new ClientRegistryBo();
        bo.setStatus(Constant.ON);
        List<ClientRegistryVo> clientRegistryVos = iClientRegistryService.queryList(bo);

        for(ClientRegistryVo temp : clientRegistryVos){
            String limits = temp.getLimits();
            if(StringUtils.isEmpty(limits)){
                continue;
            }
            //根据权限进行拉取
            if(!limits.contains("10008")){
                continue;
            }

            if(iClientService.do10008(temp)){
                operation(temp.getClientCode(), temp.getClientName(), "10008拉取", "成功");
            }else{
                operation(temp.getClientCode(), temp.getClientName(), "10008拉取", "失败");
            }
        }
    }


    private void online(String clientCode, String clientName){
        ClientLogBo bo = new ClientLogBo();
        bo.setClientCode(clientCode);
        bo.setClientName(clientName);
        iClientLogService.online(bo);
    }

    private void offline(String clientCode, String clientName){
        ClientLogBo bo = new ClientLogBo();
        bo.setClientCode(clientCode);
        bo.setClientName(clientName);
        iClientLogService.offline(bo);
    }

    private void operation(String clientCode, String clientName,String operationType,String operationDes){
        ClientLogBo bo = new ClientLogBo();
        bo.setClientCode(clientCode);
        bo.setClientName(clientName);
        bo.setOperationType(operationType);
        bo.setOperationDes(operationDes);
        bo.setCreateBy(Constant.ADMIN);
        bo.setCreateTime(new Date());
        bo.setUpdateBy(Constant.ADMIN);
        bo.setUpdateTime(new Date());
        iClientLogService.insertByBo(bo);
    }



}
